package com.n5x.service.impl;

import com.n5x.mapper.goods.GoodsGroupMapper;
import com.n5x.mapper.goods.GoodsInfoMapper;
import com.n5x.mapper.goods.GoodsInfoTypeMapper;
import com.n5x.mapper.goods.GoodsTypeMapper;
import com.n5x.model.AbstractModel;
import com.n5x.model.goods.*;
import com.n5x.service.IGoodsService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/22.
 */
@Service
@Transactional
public class IGoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    @Autowired
    private GoodsGroupMapper goodsGroupMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsInfoTypeMapper goodsInfoTypeMapper;


    //******************************************商品组别(GoodsGroup)*******************************************/

    public List<GoodsGroup> findGoodsGroupByGoodsInfoId(String fkGoodsInfoId) {
        GoodsGroupExample example = new GoodsGroupExample();
        example.createCriteria().andFkGoodsInfoEqualTo(fkGoodsInfoId);
        return goodsGroupMapper.selectByExample(example);
    }

    //******************************************商品类型(GoodsType)*******************************************/

    /**
     * 保存或更新商品类型
     *
     * @param goodsType 商品类型对象
     * @return 商品类型ID
     */
    public GoodsType saveGoodsType(GoodsType goodsType) {
        Assert.notNull(goodsType, "商品类型对象不能为空");
        if (StringUtils.isBlank(goodsType.getId())) {
            goodsType.init();
            goodsTypeMapper.insert(goodsType);
        } else {
            goodsType.setLastUpdateTime(new Date());
            goodsTypeMapper.updateByPrimaryKeySelective(goodsType);
        }
        return goodsType;
    }

    /**
     * 查询商品类型
     *
     * @param criteria 查询条件[可选]
     * @return 商品类型列表
     */
    public List<GoodsType> findGoodTypes(GoodsTypeExample.Criteria criteria) {
        GoodsTypeExample goodsTypeExample = new GoodsTypeExample();
        if (criteria == null) {
            criteria = goodsTypeExample.or();
        } else {
            goodsTypeExample.or(criteria);
        }
        criteria.andDisableEqualTo(Boolean.FALSE);
        goodsTypeExample.setOrderByClause(AbstractModel.ORDER);
        return goodsTypeMapper.selectByExample(goodsTypeExample);
    }

    /**
     * 根据商品类型ID查询商品类型对象
     *
     * @param id ID
     * @return 商品类型对象
     */
    public GoodsType findGoodTypes(String id) {
        Assert.notNull(id, "商品类型ID不能为空");
        return goodsTypeMapper.selectByPrimaryKey(id);
    }

    //******************************************商品类型(GoodsType)*******************************************/

    /**
     * 保存商品信息
     *
     * @param goodsInfo    商品信息对象
     * @param groupNames   商品关联分组名称数组
     * @param goodsTypeIds 商品类型ID数组
     * @return 保存商品ID
     */
    public GoodsInfo saveGoodsInfo(GoodsInfo goodsInfo, Enum<GoodsGroup.GoodsTypeGroupName>[] groupNames, String[] goodsTypeIds) {
        goodsInfo.setLastUpdateTime(new Date());
        boolean isNew =StringUtils.isBlank(goodsInfo.getId());
        if (isNew) {
            if(goodsTypeIds != null && StringUtils.isNotBlank(goodsTypeIds[0]) ){
                GoodsInfoTypeExample goodsInfoTypeExample = new GoodsInfoTypeExample();
                goodsInfoTypeExample.or().andFkGoodsTypeEqualTo(goodsTypeIds[0]);
                int count = goodsInfoTypeMapper.countByExample(goodsInfoTypeExample);
                String code = String.format("%02d%03d", Integer.valueOf(goodsTypeIds[0]),count+1);
                goodsInfo.setCodeId(code);
            }
            goodsInfo.init();
            goodsInfoMapper.insert(goodsInfo);
        } else {
            goodsInfo.setLastUpdateTime(new Date());
            goodsInfoMapper.updateByPrimaryKeySelective(goodsInfo);
            //清空关联商品类型
            //GoodsInfoTypeExample goodsInfoTypeExample = new GoodsInfoTypeExample();
            //goodsInfoTypeExample.or().andFkGoodsInfoEqualTo(goodsInfo.getId());
            //goodsInfoTypeMapper.deleteByExample(goodsInfoTypeExample);
            //清空关联商品分组
            GoodsGroupExample goodsGroupExample = new GoodsGroupExample();
            goodsGroupExample.or().andFkGoodsInfoEqualTo(goodsInfo.getId());
            goodsGroupMapper.deleteByExample(goodsGroupExample);
        }
        if (isNew && ArrayUtils.isNotEmpty(goodsTypeIds)) {
            for (String goodsTypeId : goodsTypeIds) {
                GoodsInfoType goodsInfoType = new GoodsInfoType();
                goodsInfoType.setFkGoodsInfo(goodsInfo.getId());
                goodsInfoType.setFkGoodsType(goodsTypeId);
                goodsInfoTypeMapper.insert(goodsInfoType);
            }
        }
        if (ArrayUtils.isNotEmpty(groupNames)) {
            for (Enum<GoodsGroup.GoodsTypeGroupName> groupNameEnum : groupNames) {
                GoodsGroup goodsGroup = new GoodsGroup(groupNameEnum, goodsInfo);
                goodsGroupMapper.insert(goodsGroup);
            }
        }
        return goodsInfo;
    }

    /**
     * 查询商品信息
     *
     * @param goodsInfoCriteria  商品信息查询条件[可选]
     * @param goodsGroupCriteria 商品分组查询条件[可选]
     * @param goodsTypeCriteria  商品类型查询条件[可选]
     * @return 商品信息列表
     */
    public List<GoodsInfo> findGoodsInfo(GoodsInfoExample.Criteria goodsInfoCriteria, GoodsGroupExample.Criteria goodsGroupCriteria, GoodsTypeExample.Criteria goodsTypeCriteria) {
        GoodsInfoExample goodsInfoExample = new GoodsInfoExample();
        if (goodsInfoCriteria == null) {
            goodsInfoCriteria = goodsInfoExample.createCriteria();
        } else {
            goodsInfoExample.or(goodsInfoCriteria);
        }
        goodsInfoCriteria.andDisableEqualTo(Boolean.FALSE);
        if (goodsGroupCriteria != null) {
            GoodsGroupExample goodsGroupExample = new GoodsGroupExample();
            goodsGroupExample.or(goodsGroupCriteria);
            goodsInfoExample.setGoodsGroupExample(goodsGroupExample);
        }
        if (goodsTypeCriteria != null) {
            GoodsTypeExample goodsTypeExample = new GoodsTypeExample();
            goodsTypeExample.or(goodsTypeCriteria);
            goodsTypeCriteria.andDisableEqualTo(Boolean.FALSE);
            goodsInfoExample.setGoodsTypeExample(goodsTypeExample);
        }
        goodsInfoExample.setOrderByClause(GoodsInfo.ORDER_FIELD_CREATE_TIME);
        goodsInfoExample.setDistinct(true);
        return goodsInfoMapper.selectByExampleWithBLOBs(goodsInfoExample);
    }

    /**
     * 查询商品信息
     *
     * @param goodsId 商品ID
     * @return 商品对象
     */
    public GoodsInfo findGoodsInfo(String goodsId) {
        return goodsInfoMapper.selectByPrimaryKey(goodsId);
    }


}
